using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using Newtonsoft.Json.Linq;
using SqlSugar;
using SqlSugar.Extensions;

namespace CoreShop.Services
{
    /// <summary>
    /// 桌位表 接口实现
    /// </summary>
    public class corecmsdiscountrulesServices : Repository<corecmsdiscountrules>, IcorecmsdiscountrulesServices
    {
        public bool InsertAsync(corecmsdiscountrules entity)
        {
            return base.Change<corecmsdiscountrules>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsdiscountrules> entity)
        {
            return base.InsertOrUpdate(entity);
        }

        #region 促销计算
        /// <summary>
        /// 促销计算(结算前计算，只计算买一送一和第二份半价等促销）
        /// </summary>
        public void SetOrderingDiscount(long orderId)
        {
            try
            {
                var where = Expressionable.Create<corecmsdiscountrules>();
                where = where.And(p => p.status == (int)GlobalEnumVars.DiscountStatus.IsCheck);
                where = where.And(p => p.startTime <= DateTime.Now);
                where = where.And(p => p.endTime >= DateTime.Now);
                if (!new corecmsdiscountrulesServices().IsAny(where.ToExpression())) return;//不存在促销

                string msql = @"update corecmsfoodordering s, corecmsdiscountrules c set s.setPrice = c.discountPrice "
                        + "where s.foodSpecId = c.foodOrCategoryId and (c.weekValues = '' or c.weekValues like '%{0}%') "
                        + "and c.startTime <= NOW() and c.endTime >= NOW() and (c.status = 1) "
                        + "order by c.id desc;";
                int dateNum = (int)DateTime.Now.DayOfWeek;
                msql = string.Format(msql, dateNum);

                base.ExecuteCommand(msql);
            }
            catch (Exception) { }
        }       

        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsdiscountrules> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsdiscountrules> QueryAsync(long id)
        {
            List<corecmsdiscountrules> list = new List<corecmsdiscountrules>();
            var where = Expressionable.Create<corecmsdiscountrules>();
            where = where.And(a => a.status == 1 && a.id > 0);
            if (id > 0)
            {
                where = where.And(a => a.discountRulesId == id);
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).ToList();

            return list;
        }

        #endregion
              

    }
}
